import '@/styles/index.scss'

import type { App } from 'vue'

import pkg from '../package.json'
import setupComponents from './components/setup'
import { setupDirectives } from './directives'
import i18n from './locales'
import router from './router'
import { setupStore } from './stores'
import { blankTime } from './utils'
import LoggerUtils from './utils/logger'
const isDev = import.meta.env.DEV

async function bootstrap(app: App) {
  // 状态管理
  setupStore(app)

  // 注册全局指令
  setupDirectives(app)
  app.use(i18n)

  // 注册路由
  app.use(router)
  if (import.meta.env.VITE_ENABLE_EXTERNAL_CONFIG === 'yes') {
    await fetch('/app.json').then(async res => {
      const appStore = useAppStore()
      const config = await res.json()
      appStore.setApi(config.api)
    })
  }
  if (isDev) {
    const ElementPlus = await import('element-plus')
    app.use(ElementPlus.default)
  }

  // 注册全局组件
  app.use(setupComponents)

  // 挂载
  app.mount('#app')

  LoggerUtils.version(
    `\CDN USER For Web \n\nversion: ${pkg.version}\nenv: ${
      import.meta.env.MODE
    }\nBlank Time: ${blankTime()}\n`
  )
}
export default bootstrap
